package api

import (
	"batbat-backend/app/model"
	"batbat-backend/app/service"
	"batbat-backend/library/response"
	"github.com/gogf/gf/net/ghttp"
)

// Captcha 验证码API接口
var Captcha = captchaApi{}

type captchaApi struct{}

// GenerateCaptcha
// @summary 生成验证码
// @tags 	验证码
// @produce json
// @success 200 {object} response.JsonResponse{data=model.CaptchaServiceGenerateResp}
// @router 	/captcha [GET]
func (c *captchaApi) GenerateCaptcha(r *ghttp.Request) {
	res, err := service.Captcha.GenerateCaptcha(r.Context())
	if err != nil {
		response.Error(r, err)
	}
	response.Success(r, res)
}

// VerifyCaptcha
// @summary 验证用户输入的验证码是否正确
// @tags 	验证码
// @accept	mpfd
// @param	id formData string true "验证码id"
// @param 	ans formData string true "用户输入的验证码结果"
// @produce json
// @success 200 {object} response.JsonResponse{data=bool}
// @router 	/captcha [POST]
func (c *captchaApi) VerifyCaptcha(r *ghttp.Request) {
	var req *model.CaptchaApiVerifyReq
	err := r.ParseForm(&req)
	if err != nil {
		response.Error(r, err)
	}
	res := service.Captcha.VerifyCaptcha(r.Context(), req.Id, req.Ans)
	response.Success(r, res)
}
